//
//  HTTPMananger.m
//  EcShop
//
//  Created by soft on 2017/10/25.
//  Copyright © 2017年 soft. All rights reserved.
//

#import "HTTPMananger.h"
#define kBaseURL @"http://cdnme.azurewebsites.net/api"
@implementation HTTPMananger

+(HTTPMananger *)sharedManager{
    static HTTPMananger * _sharedManager = nil;
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
        //        [config setHTTPAdditionalHeaders:@{@"Content-type":@"application/json"}];
        //        [config setHTTPAdditionalHeaders:@{@"Accept":@"application/json"}];
        NSURL *baseURL = [NSURL URLWithString:kBaseURL];
        
        _sharedManager = [[HTTPMananger alloc]initWithBaseURL:baseURL sessionConfiguration:config];
        _sharedManager.responseSerializer.acceptableContentTypes = [NSSet setWithArray:@[@"application/json",@"text/html",@"text/json",@"text/plain",@"text/javascript",@"text/xml",@"image/*",]];
    });
    
    return _sharedManager;
}

-(void)GET:(NSString *)urlStr parameters:(id)params callback:(HTTPDictResultBlock)callback{
    
    [self GET:urlStr parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        BOOL success = [responseObject[@"status"] boolValue];
      //  BOOL success = [responseObject[@"result"] boolValue];
        NSDictionary *result = responseObject[@"result"];
        
        if (success) {
            
            callback(result, nil);
            
        }else{
            
            callback(nil, [NSError new]);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        if (callback) {
            callback(nil, error);
            NSLog(@"接口错误, URL: %@\nerror:\n %@",task.originalRequest.URL ,error);
        }
        
    }];
}
//主页轮播
-(void)home:(HTTPDictResultBlock)callback{
    [self GET:@"home/index" parameters:nil callback:callback];
}
//-(void)productIndex:(HTTPDictResultBlock)callback pid:(NSString *)pid{
//  //  NSString *url = [NSString stringWithFormat:@"product/index?pid=%@",pid];
//    [self GET:@"product/index" parameters:@{@"pid":pid} callback:callback];
//    //[self GET:url parameters:nil callback:callback];
//}
//搜索推荐
-(void)searchRecommend:(HTTPDictResultBlock)callback{
    [self GET:@"search/recommend" parameters:nil callback:callback];
}

//-(void)categoryIndex:(HTTPDictResultBlock)callback{
//  [self GET:@"category/index" parameters:nil callback:callback];
//}
//搜索商品----返回商品数组
-(void)searchIndex:(HTTPDictResultBlock)callback productPage:(NSDictionary *)prPagedict{
    [self GET:@"Search/Index" parameters:prPagedict callback:callback];
}
//图片尺寸
-(void)imagesizeFilename:(HTTPDictResultBlock)callback imagesize:(NSDictionary *)imaSdict{
    [self GET:@"Image/ImageSize" parameters:imaSdict callback:callback];
}
-(void)newProduct:(HTTPDictResultBlock)callback product:(NSDictionary *)product{
     [self GET:@"product/newproduct" parameters:product callback:callback];
}
-(void)hotpProduct:(HTTPDictResultBlock)callback product:(NSDictionary *)product{
     [self GET:@"product/hotproduct" parameters:product callback:callback];
}
-(void)classifyProduct:(HTTPDictResultBlock)callback{
     [self GET:@"Category/index" parameters:nil callback:callback];
}
@end
